<?php
require_once 'EntreeStock.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BDDEntreeStock
 *
 * @author Gael
 */
class BDDEntreeStock extends EntreeStock{
    
    
     static public function byId($id){
        try{
            $db= getPdo();
            
            $rep = $db->query("select * from entreestock where id=$id" );
            while($data =$rep->fetch()){
                $ta= new EntreeStock($data['id'],  
                        $data['dateentree'],
                        BDDSociete::byId($data['idsociete']),
                        BDDEntrepot::byId($data['identrepot']),
                        BDDArticle::byId($data['idarticle']));
            }
            
            $rep->closeCursor();
            return $ta;
        }catch(Exception $e){
            die('erreur requete'.$e->getMessage());
            return false;
        }
    }
    static public function all(){
        try{
            $db= getPdo();
            $tab = array();
           
            $rep = $db->query("select * from entreestock  " );
            while($data =$rep->fetch()){
                
                $tab[$data['id']]=new EntreeStock($data['id'],  
                        $data['dateentree'],
                        BDDSociete::byId($data['idsociete']),
                        BDDEntrepot::byId($data['identrepot']),
                        BDDArticle::byId($data['idarticle']));
                
                
            }
            $rep->closeCursor();
            return $tab;
        }catch(Exception $e){
            die('erreur requete'.$e->getMessage());
        }
    } 
    
    
     static function entreeArticle($typearticle,$entrepot,$societe){
         
        
        try{
            $db= getPdo();
            $db->beginTransaction();
             $cle =null;
        $cle = BDDArticle::insArticle($typearticle);
            
            $rep = $db->query("select * from article where codebarre='$cle' LIMIT 1" );
            while($data =$rep->fetch()){
                $ta= new BDDArticle($data['id'], $data['codebarre'], BDDTypeArticle::byId($data['idtypearticle']));
            }
            
            $rep->closeCursor();
            
            $codebarre = date("Ymd").$ta->id;
           
            
            
            
            $ta->updateCodebarre($codebarre);
            $req =$db->prepare("INSERT INTO entreestock(id, dateentree, idarticle,identrepot,idsociete) VALUES(:id, :dateentree,:idarticle ,:identrepot,:idsociete)");
             /*if($req->execute(array('id' => NULL,'dateentree' => date("Ymd"),'idarticle' => $ta->id,'identrepot' => $entrepot,'idsociete' => $societe))){
                $es = BDDEntreeStock::all();
            p6($es); 
            }else{
                die(print_r($db->errorInfo(),true));
            }*/
           $req->execute(array('id' => NULL,'dateentree' => date("Ymd"),'idarticle' => $ta->id,'identrepot' => $entrepot,'idsociete' => $societe));
          
            $db->commit();
            }catch(Exception $e){
                $db->rollBack();
                die('erreur requete'.$e->getMessage());
                return false;
            }
        
          
        
    }
     static public function moveArticle($idarticle,$oldentrepot, $newentrepot,$idsociete){
        
        
        try{
            $nbr= null;
            //die("erreur requete UPDATE entreestock SET dateentree ='".date('Y-m-d')."', identrepot = $newentrepot WHERE idarticle =$idarticle and idsociete=$idsociete and identrepot=$oldentrepot");
                 $db=getPdo();
                 $db->beginTransaction();
                 
            $nbr =$db->exec("UPDATE entreestock SET dateentree ='".date('Y-m-d')."', identrepot = $newentrepot WHERE idarticle =$idarticle and idsociete=$idsociete and identrepot=$oldentrepot");
             $db->commit();
            if($nbr){
                 return true ;
             }else{
                 echo("erreur requete UPDATE entreestock SET dateentree ='".date('Y-m-d')."', identrepot = $newentrepot WHERE idarticle =$idarticle and idsociete=$idsociete and identrepot=$oldentrepot");
                 return null;
             }
            
            
        }catch(Exception $e){
            $db->rollBack();
            die('erreur requete'.$e->getMessage());
            return false;
        }
       
        
    }
    
    static public function byArticleId($id){
        try{
            $db= getPdo();
            $rep = $db->query("select * from entreestock where idarticle=$id" );
            $ta = array();
            while($data =$rep->fetch()){
                $ta = array("societe"=>BDDSociete::byId($data['idsociete']), "entrepot"=>BDDEntrepot::byIdEntrepot($data['identrepot']));
            }
            
            $rep->closeCursor();
            return $ta;
        }catch(Exception $e){
            die('erreur requete'.$e->getMessage());
            return false;
        }
    }

//innodb en mode serialisable
}

?>
